#define aplf(...) { char tmps[300]; sprintf(tmps,__VA_ARGS__); apl(tmps); }
#define pf(...) { char tmps[300]; sprintf(tmps,__VA_ARGS__); pl(tmps); }
#define RGBA( r, g, b, a ) ( (r<<24) | (g<<16) | (b<<8) | a )
#define ARGB( r, g, b, a ) ( (a<<24) | (r<<16) | (g<<8) | b )
#define ABGR( r, g, b, a ) ( (a<<24) | (b<<16) | (g<<8) | r )
#define sok() fcolor(ABGR(0,255,0,255)); apl(" OK! \n");
#define sfail() fcolor(RED); apl(" ERR \n");

#define WHT 0xFFFFFFFF
#define BLK 0xFF000000
#define GRY 0xFF333333
#define RED 0xFF0000FF
#define GRN 0xFF00FF00
#define YLW 0xFF00FFFF
#define BLU 0xFFFF0000



void screeninit() { pspDebugScreenInit(); }
void pc(unsigned char c) { char temp[2]; sprintf(temp,"%c",c); pspDebugScreenPrintf(temp); }
void fcolor(int color) { pspDebugScreenSetTextColor(color); }
void bcolor(int color) { pspDebugScreenSetBackColor(color); }
void acolor(int color) { fcolor(color); bcolor(color); }
void cls() { pspDebugScreenClear(); }
void sxy(int x, int y) { pspDebugScreenSetXY(x,y); }
int gx() { return pspDebugScreenGetX(); }
int gy() { return pspDebugScreenGetY(); }

void pl(char *str) {
	int i;
	for (i=0;i<strlen(str);i++)
		pc(str[i]);
}

void appkilled() {
	bcolor(BLK); fcolor(WHT); fcolor(RED); pf("\n[!] "); fcolor(WHT); pf("Presiona X para volver al XMB.");
	while (1) {
		sceCtrlReadBufferPositive(&pad,1);
		if ( pad.Buttons & PSP_CTRL_CROSS ) { sceKernelExitGame(); break; } 
		if ( pad.Buttons & PSP_CTRL_CIRCLE ) { sceKernelExitGame(); } 
	}	
}

void zzz(int numframes) { 
	int i; 
	for (i=1;i<=numframes;i++) { 
		SceCtrlData pad; 
		sceCtrlReadBufferPositive(&pad,1); 
	} 
}

void apl(char *str) { 
	int i; 
	for (i=0;i<strlen(str);i++) { 
		pc(str[i]); 
		zzz(1); 
	} 
}

void guidraw() {
	/* LINE */ sxy(0,0); acolor(GRY); pl("                                                                   ");
	/* SE_L */ sxy(0,1); acolor(GRY); pl(" "); bcolor(BLK); pl("                                                                 "); acolor(GRY); pl(" ");
	/* AUTH */ sxy(0,2); acolor(GRY); pl(" "); bcolor(BLK); fcolor(WHT); pl(" HUB driver 0.1a - (c) DeViaNTe - "); fcolor(BLU); pl(" http://psp3jb.googlecode.com  "); acolor(GRY); pl(" ");
	/* INFO */ sxy(0,3); acolor(GRY); pl(" "); bcolor(BLK); fcolor(WHT); pl(" >> A little bit of text...                                      "); acolor(GRY); pl(" ");
	/* INFO */ sxy(0,4); acolor(GRY); pl(" "); bcolor(BLK); fcolor(WHT); pl(" >> Special thanks going here...                                 "); acolor(GRY); pl(" ");
	/* SE_L */ sxy(0,5); acolor(GRY); pl(" "); bcolor(BLK); pl("                                                                 "); acolor(GRY); pl(" ");
	/* LINE */ sxy(0,6); acolor(GRY); pl("                                                                   ");
	/* SE_L */ sxy(0,7); acolor(GRY); pl(" "); bcolor(BLK); pl("                                                                 "); acolor(GRY); pl(" ");
	/* HUBL */ sxy(0,8); acolor(GRY); pl(" "); bcolor(BLK); fcolor(WHT); pl(" HUB DRIVER: "); fcolor(WHT);
	/* HUB LOADED LED */ acolor(RED); if ( localhubstatus.loaded == 1 ) { acolor(GRN); } if ( localhubstatus.loaded == 2 ) { acolor(YLW); } pc(' '); acolor(BLK); pc(' ');
	/* HUB REGGED LED */ if ( localhubstatus.regged == 1 ) { acolor(GRN); } else { acolor(RED); } pc(' '); acolor(BLK); pc(' ');
	/* HUB ACTIVE LED */ acolor(RED); if ( localhubstatus.started == 1 ) { acolor(GRN); } if ( localhubstatus.started == 2 ) { acolor(YLW); } pc(' '); acolor(BLK); pc(' ');
	/* HUB CONNEC LED */ acolor(RED); if ( res.connected == 1 ) { acolor(GRN); } if ( res.connected == 2 ) { acolor(YLW); } pc(' '); acolor(BLK); pl("                                             "); acolor(GRY); pc(' ');
	/* SE_L */ sxy(0,9); acolor(GRY); pl(" "); bcolor(BLK); pl("                                                                 "); acolor(GRY); pl(" ");
	/* HUB STATE MACH */ sxy(0,10); acolor(GRY); pl(" "); bcolor(BLK); fcolor(WHT); pl(" HUB STATE: UNKNOWN                                              "); acolor(GRY); pl(" ");	
	/* SE_L */ sxy(0,11);  acolor(GRY); pl(" "); bcolor(BLK); pl("                                                                 "); acolor(GRY); pl(" ");
	/* LINE */ sxy(0,12); acolor(GRY); pl("                                                                   ");
	/* PORT TABLE HEADER */ 
	sxy(0,13); acolor(GRY); pl(" "); 
	bcolor(BLK); fcolor(WHT); pl(" PORT # "); acolor(GRY); pc(' ');
	bcolor(BLK); fcolor(WHT); pl(" C "); acolor(GRY); pc(' ');
	bcolor(BLK); fcolor(WHT); pl(" E "); acolor(GRY); pc(' ');
	bcolor(BLK); fcolor(WHT); pl(" S "); acolor(GRY); pc(' ');
	bcolor(BLK); fcolor(WHT); pl(" O "); acolor(GRY); pc(' ');
	bcolor(BLK); fcolor(WHT); pl(" R "); acolor(GRY); pc(' ');
	bcolor(BLK); fcolor(WHT); pl(" P "); acolor(GRY); pc(' ');
	bcolor(BLK); fcolor(WHT); pl(" L "); acolor(GRY); pc(' ');
	bcolor(BLK); fcolor(WHT); pl(" H "); acolor(GRY); pc(' ');
	bcolor(BLK); fcolor(WHT); pl(" I "); acolor(GRY); pc(' ');
	bcolor(BLK); fcolor(WHT); pl("                    "); acolor(GRY); pc(' ');
	/* LINE */ sxy(0,14); acolor(GRY); pl("                                                                   ");
	int i;
	for (i=0;i<6;i++) {
		sxy(0,15+i); acolor(GRY); pc(' '); 
		bcolor(BLK); fcolor(WHT); pf(" PORT %d ",(i+1)); acolor(GRY); pc(' '); acolor(BLK); pc(' ');
		acolor(RED); if ((res.ports[i].status & PORT_STAT_CONNECTION ) == PORT_STAT_CONNECTION ) { acolor(GRN); } pc(' '); acolor(BLK); pc(' '); acolor(GRY); pc(' '); acolor(BLK); pc(' ');
		acolor(RED); if ((res.ports[i].status & PORT_STAT_ENABLE ) == PORT_STAT_ENABLE) { acolor(GRN); } pc(' '); acolor(BLK); pc(' '); acolor(GRY); pc(' '); acolor(BLK); pc(' ');
		acolor(RED); if ((res.ports[i].status & PORT_STAT_SUSPEND ) == PORT_STAT_SUSPEND ) { acolor(GRN); } pc(' '); acolor(BLK); pc(' '); acolor(GRY); pc(' '); acolor(BLK); pc(' ');
		acolor(RED); if ((res.ports[i].status & PORT_STAT_OVER_CURRENT ) == PORT_STAT_OVER_CURRENT ) { acolor(GRN); } pc(' '); acolor(BLK); pc(' '); acolor(GRY); pc(' '); acolor(BLK); pc(' ');
		acolor(RED); if ((res.ports[i].status & PORT_STAT_RESET ) == PORT_STAT_RESET ) { acolor(GRN); }	pc(' '); acolor(BLK); pc(' '); acolor(GRY); pc(' '); acolor(BLK); pc(' ');
		acolor(RED); if ((res.ports[i].status & PORT_STAT_POWER )) { acolor(GRN); } pc(' '); acolor(BLK); pc(' '); acolor(GRY); pc(' '); acolor(BLK); pc(' ');
		acolor(RED); if ((res.ports[i].status & PORT_STAT_LOW_SPEED ) == PORT_STAT_LOW_SPEED ) { acolor(GRN); }	pc(' '); acolor(BLK); pc(' '); acolor(GRY); pc(' '); acolor(BLK); pc(' ');
		acolor(RED); if ((res.ports[i].status & PORT_STAT_HIGH_SPEED ) == PORT_STAT_HIGH_SPEED ) { acolor(GRN); } pc(' '); acolor(BLK); pc(' '); acolor(GRY); pc(' '); acolor(BLK); pc(' ');
		bcolor(BLK); fcolor(WHT); pl("  "); acolor(GRY); pc(' '); acolor(BLK); pc(' ');
		bcolor(BLK); fcolor(WHT); pl("                   "); acolor(GRY); pc(' ');
	}
	/* LINE */ sxy(0,21); acolor(GRY); pl("                                                                   ");
	if ( endtime > 0 ) {
		/* SE_L */ sxy(0,22); acolor(GRY); pc(' ');
		clock_t total = timeout;
		clock_t rest = (endtime - now);
		int d = (rest * 65) / timeout;
		acolor(BLU);
		for (i=0;i<(65-d);i++) { pc(' '); }
		acolor(0xFF111111);
		for (i=(65-d);i<65;i++) { pc(' '); }
		acolor(GRY); pc(' ');
	}
	else { 
		/* SE_L */ sxy(0,22); acolor(GRY); pl(" "); acolor(0xFF111111); pl("                                                                 "); acolor(GRY); pl(" ");
	}
	/* LINE */ sxy(0,23); acolor(GRY); pl("                                                                   ");

	
	/* OTHER */
	bcolor(BLK);
	sxy(0,25);
}